package com.uca.authority.conf.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.uca.authority.conf.domain.UcaConf;
import com.uca.authority.conf.service.IUcaConfService;
import com.uca.common.core.controller.BaseController;
import com.uca.common.utils.DateUtils;
import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;


/**
 * UCA-登陆APIController
 * 
 * @author 刘殿生
 * @date 2021-03-30
 */
@RestController
@RequestMapping("/uca/auth")
public class UcaAuthController extends BaseController
{
    private String prefix = "uca/auth";
	private static int DEFAULT_KEY_SIZE=256;

    @Autowired
    private IUcaConfService ucaConfService;
    /**
     * 登陆接口
     * @return {@link Result}
     * @throws IOException 
     */
    @GetMapping("/login")
    public void   login(HttpServletResponse response,HttpServletRequest request) throws IOException {
         //AES:Advanced Encryption Standard的缩写,高级加密标准 比DES加密更难以破解

         System.out.println("getRequestURL: "+request.getRequestURL());
         System.out.println("getRequestURI: "+request.getRequestURI());
         System.out.println("getQueryString: "+request.getQueryString());
         System.out.println("getRemoteAddr: "+request.getRemoteAddr());
         System.out.println("getRemoteHost: "+request.getRemoteHost());
         System.out.println("getRemotePort: "+request.getRemotePort());
         System.out.println("getRemoteUser: "+request.getRemoteUser());
         System.out.println("getLocalAddr: "+request.getLocalAddr());
         System.out.println("getLocalName: "+request.getLocalName());
         System.out.println("getLocalPort: "+request.getLocalPort());
         System.out.println("getMethod: "+request.getMethod());
         
    	 // 获取系统标识
         String sourceFlag = request.getParameter("sys");
         UcaConf ucaConf =  ucaConfService.selectUcaConfBysourceFlag(sourceFlag);         
         // 获取私钥，再次利用私钥对url连接进行加盐处理
    	 byte[] key_decode = Base64Decoder.decode(ucaConf.getPrivateKey());
    	//构建 aes
    	 AES aes = SecureUtil.aes(key_decode);
    	 // 进行参数获取
         String query[] = request.getQueryString().split("&");
        //解密
     	 String query_decr = aes.decryptStr(Base64Decoder.decode(query[0]));
     	 
        
         // 时间盐
         String solt_date=DateUtils.getTime();
         
         // url拼接
         StringBuffer url_buffer = new StringBuffer();
         url_buffer.append(ucaConf.getTarget());
         url_buffer.append("?");
         
         // 盐URL加密及拼接
         StringBuffer url_buffer_solt = new StringBuffer();
         url_buffer_solt.append(query_decr);  //获取已经解密的参数
         url_buffer_solt.append("&solt_date=");
          url_buffer_solt.append(solt_date); // 时间盐
         //加密
         byte[] encrypt = aes.encrypt(url_buffer_solt.toString()); 
         String url_solt = Base64Encoder.encode(encrypt);
         
         url_buffer.append(url_solt);
         url_buffer.append("&sys=") ;
         url_buffer.append(ucaConf.getSourceFlag()) ;
      	 response.sendRedirect(url_buffer.toString());
    	
    } 
   
    
}
